﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CompoundBindService.cs" company="">
//   
// </copyright>
// <summary>
//   The compound bind service.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Bindings
{
    using System.Collections.Generic;

    using Libium.Adapters;

    /// <summary>
    ///   The compound bind service.
    /// </summary>
    public class CompoundBindService : IBindService
    {
        #region Fields

        /// <summary>
        ///   The _bind services.
        /// </summary>
        private readonly List<IBindService> _bindServices = new List<IBindService>();

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets or sets the adapt service.
        /// </summary>
        public IAdaptService AdaptService { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The add bind service.
        /// </summary>
        /// <param name="service">
        /// The service. 
        /// </param>
        public void AddBindService(BindService service)
        {
            service.Parent = this;
            service.AdaptService = this.AdaptService;
            this._bindServices.Add(service);
        }

        /// <summary>
        /// The try bind.
        /// </summary>
        /// <param name="obj1">
        /// The obj 1. 
        /// </param>
        /// <param name="obj2">
        /// The obj 2. 
        /// </param>
        /// <param name="binding">
        /// The binding. 
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> . 
        /// </returns>
        public virtual bool TryBind(object obj1, object obj2, out IBinding binding)
        {
            binding = null;

            foreach (var bindService in this._bindServices)
            {
                if (bindService.TryBind(obj1, obj2, out binding))
                {
                    break;
                }
            }

            return binding != null;
        }

        /// <summary>
        /// The try unbind.
        /// </summary>
        /// <param name="obj1">
        /// The obj 1. 
        /// </param>
        /// <param name="obj2">
        /// The obj 2. 
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> . 
        /// </returns>
        public virtual bool TryUnbind(object obj1, object obj2)
        {
            foreach (var bindService in this._bindServices)
            {
                if (bindService.TryUnbind(obj1, obj2))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// The try unbind.
        /// </summary>
        /// <param name="obj">
        /// The obj. 
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> . 
        /// </returns>
        public virtual bool TryUnbind(object obj)
        {
            foreach (var bindService in this._bindServices)
            {
                if (bindService.TryUnbind(obj))
                {
                    return true;
                }
            }

            return false;
        }

        #endregion
    }
}